package main

import (
	"ddy/gin/res"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func jsonResTest(c *gin.Context) {
	typ, ok1 := c.GetQuery("type")
	if !ok1 {
		res.FailWithMsg(c, "type 未定义")
		return
	}
	msg, ok2 := c.GetQuery("msg")
	data, ok3 := c.GetQueryMap("data")
	code, ok4 := c.GetQuery("code")
	if typ == "fail" && ok4 {
		v, _ := strconv.Atoi(code)
		res.FailWithCode(c, v)
		return
	}

	if ok2 && ok3 {
		if typ == "ok" {
			res.Ok(c, msg, data)
		}
		if typ == "fail" {
			res.Fail(c, msg, data)
		}
		return
	}
	if ok2 {
		if typ == "ok" {
			res.OkWithMsg(c, msg)
		}
		if typ == "fail" {
			res.FailWithMsg(c, msg)
		}
		return
	}
	if ok3 && typ == "ok" {
		res.OkWithData(c, data)
		return
	}
	res.FailWithCode(c, 3001)
}
func main() {
	router := gin.Default()

	router.GET("/hello", func(c *gin.Context) {
		c.String(http.StatusOK, "hello world!")
	})

	router.GET("/jsonRtest", jsonResTest)

	router.Run(":8000")
}
